package dp.gr03.payroll.classificationTransaction;

import java.util.GregorianCalendar;

import dp.gr03.payroll.classifications.CommissionedClassification;
import dp.gr03.payroll.classifications.SalesReceipt;
import dp.gr03.payroll.exception.PayRollException;
import dp.gr03.payroll.payrollApplication.Transaction;
import dp.gr03.payroll.payrollDatabase.PayrollDatabase;
import dp.gr03.payroll.payrollDomain.Employee;
import dp.gr03.payroll.payrollDomain.PaymentClassification;

public class SalesReceiptTransaction implements Transaction
{

    int empID;
    GregorianCalendar date;
    double amount;

    public SalesReceiptTransaction(int empID, GregorianCalendar date, double amount)
    {
        this.empID = empID;
        this.date = date;
        this.amount = amount;
    }

    @Override
    public void execute() throws PayRollException
    {
        Employee e = PayrollDatabase.getInstance().getEmployee(empID);
        if (e != null)
        {
            PaymentClassification pc = e.getPaymentClassification();
            if (pc instanceof CommissionedClassification)
            {
                CommissionedClassification cc = (CommissionedClassification) pc;
                cc.addSalesReceipt(new SalesReceipt(date, amount));
            } else
            {
                throw new PayRollException("Tried to add salesreceipt to non-commissioned employee");
            }
        } else
        {
            throw new PayRollException("No such employee");
        }
    }

}
